﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenCommon.ComMethod;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using com.yeepay.bank;

namespace FortuneHeaven.Web.Pages.Charge
{
    public partial class ChargeNetPay : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.charge);
        }

        public void ChargeValue(object sender, EventArgs e)
        { 
             string username =this.txtUser.Text.Trim();
            string doublename =this.txtDUser.Text.Trim();
            string validatecode =this.txtValidate.Text.Trim().ToLower();
            string value = this.txtValue.Text.Trim();
            DateTime time = DateTime.Now;
            try
            {
                if (Session[ValidationCode.ChargeName] == null)
                {
                    ShowCurError("验证码错误");
                    return;
                }
                if (validatecode.ToLower() != ((string)WebSession.Session[ValidationCode.ChargeName]).ToLower())
                {
                    ShowCurError("验证码错误");
                    return;
                }
                try
                {
                    int.Parse(value);
                }
                catch
                {
                    ShowCurError("金额请输入有效数字");
                    return;
                }
                if (username != doublename)
                {
                    ShowCurError("用户名两次输入不同");
                    return;
                }
                AccountsInfoBO bo = new AccountsInfoBO();
                AccountsInfo acount = bo.GetLightAcountInfor(username);
                if (acount == null)
                {
                    ShowCurError("该用户名不存在");
                    return;
                }

                ChargeSetValue(username, value);

            }
            catch
            {
                ShowCurError("服务器异常");
            }
        }

        public void ChargeSetValue(string user, string value)
        {

            string p2_Order;
            string p3_Amt;
            string p4_Cur;
            string p5_Pid;
            string p6_Pcat;
            string p7_Pdesc;
            string p8_Url;
            string p9_SAF;
            string pa_MP;
            string pd_FrpId;
            string pr_NeedResponse;

            // 设置 Response编码格式为GB2312
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            // 商户订单号,选填.
            //若不为""，提交的订单号必须在自身账户交易中唯一;为""时，易宝支付会自动生成随机的商户订单号.
            p2_Order = "";

            // 支付金额,必填.
            //单位:元，精确到分.                   
            p3_Amt = value;

            //交易币种,固定值"CNY".
            p4_Cur = "CNY";

            //商品名称
            //用于支付时显示在易宝支付网关左侧的订单产品信息.
            p5_Pid = "财富天堂-会员充值";

            //商品种类
            p6_Pcat = "财富天堂-会员充值";

            //商品描述
            p7_Pdesc = "财富天堂-会员充值";

            //商户接收支付成功数据的地址,支付成功后易宝支付会向该地址发送两次成功通知.
            string cftt_root = System.Configuration.ConfigurationManager.AppSettings["cftt_root"];
            p8_Url = Request.Url.GetLeftPart(UriPartial.Authority) + Page.ResolveUrl("~/Pages/Charge/ChargeCallBack.aspx");

            //送货地址
            //为“1”: 需要用户将送货地址留在易宝支付系统;为“0”: 不需要，默认为 ”0”.
            p9_SAF = "0";

            //商户扩展信息
            //商户可以任意填写1K 的字符串,支付成功时将原样返回.	
            pa_MP = user;

            //银行编码
            //默认为""，到易宝支付网关.若不需显示易宝支付的页面，直接跳转到各银行、神州行支付、骏网一卡通等支付页面，该字段可依照附录:银行列表设置参数值.
            pd_FrpId = "";

            //应答机制
            //默认为"1": 需要应答机制;
            pr_NeedResponse = "1";

            string url = Buy.CreateBuyUrl(p2_Order, p3_Amt, p4_Cur, p5_Pid, p6_Pcat, p7_Pdesc, p8_Url, p9_SAF, pa_MP, pd_FrpId, pr_NeedResponse);

            Response.Redirect(url);

        }
    }
}